﻿using System;
using System.IO;
using TemperatureConverter;   
using System.Collections.Generic;


namespace Presentation
{
    public class Presenter
    {
        private IView view;
        private ILog log;

        private Temperature.TemperatureType toType;
        private Temperature.TemperatureType fromType;
        private double fromValue;
        private List<string> tempListLog; 

        private static readonly Dictionary<string, Temperature.TemperatureType> TransformType = new Dictionary
            <string, Temperature.TemperatureType>
            {
                {"Цельсия", Temperature.TemperatureType.C},
                {"C", Temperature.TemperatureType.C},
                {"Кельвина", Temperature.TemperatureType.K},
                {"K", Temperature.TemperatureType.K},
                {"Фаренгейта", Temperature.TemperatureType.F},
                {"F", Temperature.TemperatureType.F},
                {"Ньютона", Temperature.TemperatureType.N},
                {"N", Temperature.TemperatureType.N}
            };

        public Presenter(IView view, ILog log)
        {
            this.log = log;
            Rebind(view);
        }

         public void Rebind(IView _view)
         {
             view = _view;
             
             _view.OnConvertClick += ConvertClick;
             _view.OnWriteLogToFile += WriteLogToFile;
             _view.OnHtmlClick += ChangeLogToHtml;
             _view.OnTxtClick += ChangeLogToTxt;
         }

        private void TransformInputValueToDouble()
        {
            fromValue = Convert.ToDouble(view.FromValue);
        }

        private void TransformInputTypeToTemperatureType()
        {
            fromType = TransformType[view.FromType];
            toType = TransformType[view.ToType];     
        }

        private void ParseInput()
        {
            TransformInputValueToDouble();
            TransformInputTypeToTemperatureType();
        }

        private void TryConvert()
        {
            view.ClearError();
            ParseInput();

            var from = new Temperature(fromValue, fromType);
            var toTemperature = TemperatureConverter.TemperatureConverter.Convert(from, toType);
            view.ToValue = Convert.ToString(toTemperature.Value);
            AddRecord("(" + fromValue + "," + fromType + ") = (" + toTemperature.Value + "," + toType + ")");
        }

        private void ConvertClick()
        {
            try
            {
                TryConvert();
            }

            catch (FormatException)
            {
                view.ErrorMessage = ErrorMessage.IncorrectValue;
                AddRecord(ErrorMessage.IncorrectValue);
            }

            catch (KeyNotFoundException)
            {
                view.ErrorMessage = ErrorMessage.IncorrectNameTemperature;
                AddRecord(ErrorMessage.IncorrectNameTemperature);
            }

            catch (TemperatureUnlimitedException)
            {
                view.ErrorMessage = ErrorMessage.UnlimitedTemperature;
                AddRecord(ErrorMessage.UnlimitedTemperature);
            }

            catch (FileNotFoundException)
            {
                view.ErrorMessage = ErrorMessage.FileNotFound;
                AddRecord(ErrorMessage.FileNotFound);
            }
        }

        private void AddRecord(String message)
        {
            view.Action = DateTime.Now.ToString() + ": " + message;
            log.Add(DateTime.Now.ToString() + ": " + message);
        }

        private void ChangeLogToHtml()
        {
            tempListLog = log.PrintLog();
            log = view.CreateHtmlLogger();
            log.CopyLogRecords(tempListLog);
            AddRecord("Формат журнала html");
        }

        private void ChangeLogToTxt()
        {
            tempListLog = log.PrintLog();
            log = view.CreateTxtLogger();
            log.CopyLogRecords(tempListLog);
            AddRecord("Формат журнала txt");
        }

        private void WriteLogToFile()
        {
            view.ClearError();

            try
            {
                log.WriteLog(view.FileName);
                AddRecord("Лог записан в файл " + view.FileName);

            }
            catch (FileNotFoundException)
            {
                view.ErrorMessage = ErrorMessage.FileNotFound;
                AddRecord(ErrorMessage.FileNotFound);
            }        
        }
    }
}
